package com.spark.sparkanswerserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spark.sparkanswerserver.pojo.DateEnity;
import com.spark.sparkanswerserver.pojo.NodeEntity;
import com.spark.sparkanswerserver.service.NodeService;
import com.spark.sparkanswerserver.utils.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2023/2/22 14:30
 */
@RestController
@RequestMapping("answer")
public class AnswerController {
    @Autowired
    private NodeService nodeService;

    /**
     * 病因
     * @param answer
     * @return
     */
    @GetMapping("reason")
    @PreAuthorize("hasAnyAuthority('3')")
    public Result Answer(@Param("answer")String answer) {
        System.out.println(answer.substring(0,2));
        if (answer.substring(0,2).equals("你好")){
            return Result.ok("你好，有什么可以帮您的吗？");
        }
        String s = nodeService.MatchReason(answer);
        return Result.ok(s);
    }

    @GetMapping("tu")
    @PreAuthorize("hasAnyAuthority('3')")
    public Result Tu(@Param("name")String name) {
        DateEnity dateEnity = nodeService.GetNode(name);
        return Result.ok(dateEnity);
    }
    @DeleteMapping("delete")
    @PreAuthorize("hasAnyAuthority('1')")
    public Result DeleteByName(@Param("name") String name){
        System.out.println(name);
        boolean b = nodeService.DeleteNode(name);
        if (b) {
            return Result.ok("删除成功");
        }else {
            return Result.err(311,"删除失败");
        }
    }

    @GetMapping("create")
    @PreAuthorize("hasAnyAuthority('1')")
    public Result Create(@Param("name") String name){
        if (nodeService.MatchNumber(name)!=0){
            return Result.err(312,"节点数据已存在");
        }
        try {
            nodeService.CreateNode(name);
        }catch (Exception e){
            return Result.err(313,"创建失败");
        }

        return Result.ok("创建成功");
    }

    @PostMapping("relation")
    @PreAuthorize("hasAnyAuthority('1')")
    public Result Relation(@RequestBody Map<String,Object> map){
        return null;
    }

    @GetMapping("all")
    @PreAuthorize("hasAnyAuthority('1')")
    public Result All(@RequestParam Map<String,Object> map){
        Page page = nodeService.MatchAllNodes(map);
        return Result.ok(page);
    }

}
